﻿using System;
using System.Collections.Generic;
using System.Web.Mvc;
using IterationLife.Extensions.MvcApi;
using IterationLife.Mobile.AppServices.Dtos.Football;
using IterationLife.Mobile.AppServices.Services.Football;
using Util;
using Util.Domains.Contexts;

namespace IterationLife.Mobile.ApiGateway.Areas.Football.Controllers
{
    public class TrainController : MvcApiController
    {
        private readonly IFootballTrainService _footballTrainService;
        public TrainController(IFootballTrainService footballTrainService)
        {
            _footballTrainService = footballTrainService;
        }


        public ActionResult GetHomepageData()
        {
            var userId = ApplicationContext.User.UserId.ToGuid();
            var recommendedCourses = _footballTrainService.GetRecommendedCourses(userId);
            var hotPositions = _footballTrainService.GetHotPositions();
            var hotCourseCategories = _footballTrainService.GetHotCourseCategories();

            TrainPageOutput result = new TrainPageOutput
            {
                RecommendedCourses = recommendedCourses,
                HotCourseCategories = hotCourseCategories,
                HotPositions = hotPositions,
            };

            return Ok(result);
        }

        public ActionResult GetCourseList(int page, int size, Guid? courseCategoryId = null, string positionCode = null)
        {
            Guid? userId = ApplicationContext.User.UserId.ToGuidOrNull();
            var result = _footballTrainService.GetPagedCourses(page, size, userId, courseCategoryId, positionCode);
            

            return Ok(result);
        }

        public ActionResult GetCourseDetail(Guid courseId)
        {
            Guid? userId = ApplicationContext.User.UserId.ToGuidOrNull();
            CourseDetailOutput result = _footballTrainService.GetCourseDatail(courseId, userId);

            return Ok(result);
        }

        public ActionResult GetLessonDetail(Guid lessionId)
        {
            LessonDetailOutput result = _footballTrainService.GetLessonDetail(lessionId);
            return Ok(result);
        }

        public ActionResult GetUserCourses()
        {
            var userId = ApplicationContext.User.UserId.ToGuid();
            List<CourseOutput> result = _footballTrainService.GetUserCourses(userId);
            return Ok(result);
        }

        [MvcApiAuthorize]
        [HttpPost]
        public ActionResult JoinToUserCourses(Guid courseId)
        {
            var userId = ApplicationContext.User.UserId.ToGuid();
            _footballTrainService.JoinToUserCourses(userId, courseId);
            return Ok();
        }

        [MvcApiAuthorize]
        [HttpPost]
        public ActionResult RemoveFromUserCourses(Guid courseId)
        {
            var userId = ApplicationContext.User.UserId.ToGuid();
            _footballTrainService.RemoveFromUserCourses(userId, courseId);
            return Ok();
        }

        [HttpGet]
        public ActionResult GetAllPlayerPositions()
        {
            var playerPositions = _footballTrainService.GetAllPlayerPositions();
            return Ok(playerPositions);
        }
    }
}